package com.lpf.www.redis;

import com.lpf.www.common.entity.User;
import com.lpf.www.common.utils.SerializeObjectTool;
import org.apache.commons.beanutils.BeanMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.Map;

/**
 * @Author lpf
 * @Date 2022/1/8 下午10:39
 * @ProjectName live-streaming-os
 * @Version 1.0
 * @Description:
 */
@Service
public class UserRedis {
    private Jedis jedis;

    @Autowired
    public void setJedis(Jedis jedis) {
        this.jedis = jedis;
    }

    public void saveUserInfoToJedis(User user) {
        byte[] serialize = SerializeObjectTool.serialize(user);
        jedis.set(user.getUsername().getBytes(), serialize);
        jedis.expire(user.getUsername(), 60 * 60l);
    }

    public User getUserInfoFromJedis(String key) {
        if (jedis.exists(key.getBytes())) {
            byte[] bytes = jedis.get(key.getBytes());
            Object unserialize = SerializeObjectTool.unserialize(bytes);
            return (User) unserialize;
        }
        return null;
    }
}
